package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;

import controller.DataBaseInteactor;

import model.Conference;
import model.ProgramChair;

/**
 * The view of the user when he/she login as Program Chair role.
 * 
 * @author Richard Lee
 * @version 52813
 */
public class ProgramChairView extends JFrame {

	/**
	 * Default serial verions id.
	 */
	private static final long serialVersionUID = 1252053989164897754L;
	
	/**
	 * The conference user belongs.
	 */
	private Conference conference;
	
	/**
	 * Conference title font.
	 */
	private final Font titleFont = new Font("Verdana", Font.BOLD, 20);
	
	/**
	 * GUI component.
	 */
	private final JLabel titleLabel = new JLabel();
	private final JLabel dateLabel = new JLabel();
	private final JPanel buttonsPanel = new JPanel(new FlowLayout());
	private final JButton acceptsButton = new JButton("Accept");
	private final JButton denyButton = new JButton("Deny");
	private final JButton assignSubButton = new JButton("Assign SubProgrmChair");
	private final JButton logoutButton = new JButton("Log out");
	private final JButton viewButton = new JButton("View");
	private final JButton assignManToSubButton = new JButton("Assign Manuscript");
	private final JPanel titlePanel = new JPanel(new BorderLayout());
	
	/**
	 * user id.
	 */
	private final String id;
	
	/**
	 * Test table contents.
	 */
	private Object[][] manuscriptData = {{"Title1", "Nick", "NOT REVIEWED",  "YES", "NO"},
			{"Title2", "Yang", "ACCEPTED", "YES", "YES"}};
	
	/**
	 * Manuscript table,
	 */
	private JTable manTabel;
	
	/**
	 * Database interactor.
	 */
	private final DataBaseInteactor inteactor;
	
	/**
	 * Constructor.
	 * 
	 * @param theConf the conference.
	 * @param theInteactor database interactor.
	 * @param theId user id.
	 */
	public ProgramChairView(Conference theConf, DataBaseInteactor theInteactor, String theId) {
		super("Program Chair");
		conference = theConf;
		inteactor = theInteactor;
		id = theId;
	}
	
	/**
	 * Combine and add all the GUI component.
	 */
	public void createProgramChairView() {
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		StringBuilder sb = new StringBuilder();
		sb.append("Manuscript Due Date: ");
		sb.append(conference.getManDate());
		sb.append(" Review Due Date: ");
		sb.append(conference.getreviewDate());
		sb.append(" Recommendation Due Date: ");
		sb.append(conference.getRecomDate());
		dateLabel.setText(sb.toString());
		titleLabel.setText(conference.getName());
		titleLabel.setFont(titleFont);
		titlePanel.add(titleLabel, BorderLayout.CENTER);
		titlePanel.add(dateLabel, BorderLayout.PAGE_END);
		p.add(titlePanel);
		String[] columnNames = {"Title", "Author", "Stauts", "Review", "Recomendation"};
		manTabel = new JTable(manuscriptData, columnNames);
		buttonsPanel.add(viewButton);
		buttonsPanel.add(acceptsButton);
		buttonsPanel.add(denyButton);
		buttonsPanel.add(assignSubButton);
		buttonsPanel.add(assignManToSubButton);
		buttonsPanel.add(logoutButton);
		p.add(manTabel.getTableHeader());
		p.add(manTabel);
		p.add(buttonsPanel);
		add(p);
		pack();
		setVisible(true);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		actionButton();
	}
	
	/**
	 * Add actions to buttons.
	 */
	private void actionButton() {
		acceptsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = manTabel.getSelectedRow();
				String author = (String) manuscriptData[x][1];
				String manTitle = (String) manuscriptData[x][0];
				ProgramChair pc = new ProgramChair(id, inteactor);
				pc.setStatus(conference, author, manTitle, "Accepted");
			}
		});
		
		denyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = manTabel.getSelectedRow();
				String author = (String) manuscriptData[x][1];
				String manTitle = (String) manuscriptData[x][0];
				ProgramChair pc = new ProgramChair(id, inteactor);
				pc.setStatus(conference, author, manTitle, "Denied");
			}
		});
		
		assignSubButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = manTabel.getSelectedRow();
				String author = (String) manuscriptData[x][1];
				String manTitle = (String) manuscriptData[x][0];
				AssignSubProgramChairView aspc = new AssignSubProgramChairView(id, author, manTitle, 
						conference, inteactor);
				aspc.createAssignView();
			}
		});
		
		assignManToSubButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = manTabel.getSelectedRow();
				String author = (String) manuscriptData[x][1];
				String manTitle = (String) manuscriptData[x][0];
				AssignManToSubView amtsv = new AssignManToSubView(id, author, manTitle, conference, inteactor);
				amtsv.createAssignView();
			}
		});
		
		viewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = manTabel.getSelectedRow();
				String author = (String) manuscriptData[x][1];
				String manTitle = (String) manuscriptData[x][0];
				ViewView view = new ViewView(author, manTitle, conference, inteactor);
				view.createViewView();
			}
		});
		
		logoutButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				LogInFrame loginframe = new LogInFrame(inteactor);
				loginframe.setVisible(true);
			}
		});
	}
}
